#include "sylar/head.h"
#include "sylar/iomanager.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>

sylar::Logger::ptr g_logger = SYLAR_LOG_ROOT();
int sock = 0;

void test_fiber(){
    SYLAR_LOG_INFO(g_logger) << "test_fiber";
}

void test1(){
    sylar::IOManager iom(2);
    
	sock = socket(AF_INET, SOCK_STREAM, 0);
	fcntl(sock, F_SETFL, O_NONBLOCK);
	
	sockaddr_in addr;
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(80);
	inet_pton(AF_INET, "180.101.49.11", &addr.sin_addr.s_addr);

	if(!connect(sock, (const sockaddr*)&addr, sizeof(addr)));
	else if(errno == EINPROGRESS){
		SYLAR_LOG_INFO(g_logger) << "add event errno=" << errno << " " << strerror(errno);
		sylar::IOManager::GetThis()->addEvent(sock, sylar::IOManager::READ, [](){
			SYLAR_LOG_INFO(g_logger) << "read callback";
		});
		sylar::IOManager::GetThis()->addEvent(sock, sylar::IOManager::WRITE, [](){
			SYLAR_LOG_INFO(g_logger) << "write callback";
			sylar::IOManager::GetThis()->cancelEvent(sock, sylar::IOManager::READ);
			close(sock);
		});
	}
	else{
		SYLAR_LOG_ERROR(g_logger) << "else errno=" << errno << " " << strerror(errno); 
	}

}
sylar::Timer::ptr timer;
void test_timer(){
	sylar::IOManager iom(2);
	timer = iom.addTimer(1000, [](){
		static int i = 0;
		SYLAR_LOG_INFO(g_logger) << "hello timer";
		if(++i == 5){
			timer->reset(2000, true);
		}
	}, true);
}

int main()
{
    test_timer();


    return 0;
}